package com.example.yls.listview_sqlite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

/**
 * Created by yls on 2018/11/24.
 */

public class StuAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDataChangeListener listener;

    public StuAdapter(Context context, ArrayList<Student>
            studentArrayList,IOnDataChangeListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
     if(view == null){
         view = LayoutInflater.from(context).inflate(
                 R.layout.stu_item,viewGroup,false
         );

         StuViewHolder viewHolder = new StuViewHolder();
           viewHolder.tvStuage = view.findViewById(R.id.txt_stuage);
           viewHolder.tvStuname = view.findViewById(R.id.txt_stuname);
           viewHolder.tvStuno = view.findViewById(R.id.txt_stuno);
           viewHolder.ivDel = view.findViewById(R.id.iv_del);
            view.setTag(viewHolder);

     }
      final Student student = studentArrayList.get(i);
      StuViewHolder viewHolder = (StuViewHolder) view.getTag();
       viewHolder.tvStuno.setText(student.getStuno());
       viewHolder.tvStuname.setText(student.getStuname());
       viewHolder.tvStuage.setText(String.valueOf(student.getStuage()));
        viewHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              listener.del(student);
            }
        });


        return view;
    }
}
